package com.yuntongxun.ecdemo.voicechat.ui.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.yuntongxun.ecdemo.common.CCPAppManager;
import com.yuntongxun.ecdemo.common.utils.DateUtil;
import com.yuntongxun.ecdemo.common.utils.ECPreferenceSettings;
import com.yuntongxun.ecdemo.common.utils.ECPreferences;
import com.yuntongxun.ecdemo.common.utils.LogUtil;
import com.yuntongxun.ecdemo.core.ClientUser;
import com.yuntongxun.ecdemo.storage.ContactSqlManager;
import com.yuntongxun.ecdemo.ui.chatting.IMChattingHelper;
import com.yuntongxun.ecdemo.ui.contact.ECContacts;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;
import com.yuntongxun.ecdemo.voicechat.util.PreferenceUtils;
import com.yuntongxun.ecsdk.ECDevice;
import com.yuntongxun.ecsdk.ECError;
import com.yuntongxun.ecsdk.PersonInfo;
import com.yuntongxun.ecsdk.SdkErrorCode;

import java.io.InvalidClassException;

/**
 * Created by Administrator on 2016/12/6.
 */

public class UpdateInfoService extends Service {

    UserInfo info;
    @Override
    public void onCreate() {
        super.onCreate();

    }
    StringBuffer message= new StringBuffer();
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtil.e("start","start");
        String data= PreferenceUtils.getPrefString(this, Contants.Preference.user_info,"");
        if (!data.equals("")){
            JsonHelper<UserInfo> jsonHelper = new JsonHelper<UserInfo>(UserInfo.class);
            info = jsonHelper.getData(data, "result");
            if (info!=null){

                message.append("{\"result\":" );
                message.append("{\"accountName\":\"" + info.getAccountName() + "\",");
                message.append("\"nickName\":\"" + info.getNickName() + "\",");
                message.append("\"id\":\"" + info.getId() + "\",");
                message.append("\"gender\":\"" + info.getGender() + "\",");
                message.append("\"age\":\"" + info.getAge() + "\",");
                message.append("\"country\":\"" + info.getCountry() + "\",");
                message.append("\"nativeLanguage\":\"" + info.getNativeLanguage() + "\",");
                message.append("\"learningLanguage\":\"" + info.getLearningLanguage() + "\"}}");
//                final String message = info.getNickName() + "-" + info.getGender() + "-" + info.getAge() + "-" + info.getCountry() + "-" + info.getNativeLanguage() + "-" + info.getLearningLanguage();
                final PersonInfo.Sex clientSex = PersonInfo.Sex.MALE;
                PersonInfo personInfo =new PersonInfo();
                personInfo.setBirth("2016-11-24");
                personInfo.setNickName(info.getNickName() + "(" + message.toString()+ ")");
                personInfo.setSex(clientSex);
                personInfo.setSign("hahahaha");



                ECDevice.setPersonInfo(personInfo, new ECDevice.OnSetPersonInfoListener() {
                    @Override
                    public void onSetPersonInfoComplete(ECError e, int version) {
                        IMChattingHelper.getInstance().mServicePersonVersion = version;
                        if (SdkErrorCode.REQUEST_SUCCESS == e.errorCode) {
                            try {
                                ClientUser clientUser = CCPAppManager.getClientUser();
                                if (clientUser != null) {

                                    clientUser.setUserName(info.getNickName() + "(" + message.toString()+ ")");
                                    clientUser.setSex(clientSex.ordinal() + 1);
                                    clientUser.setBirth(DateUtil.getDateMills(2016,11,24));
                                    clientUser.setpVersion(version);
                                    clientUser.setSignature("hahahaha");
                                    CCPAppManager.setClientUser(clientUser);
                                    ECContacts contacts = new ECContacts();
                                    contacts.setClientUser(clientUser);
                                    ECPreferences.savePreference(ECPreferenceSettings.SETTINGS_REGIST_AUTO, clientUser.toString(), true);
                                    ContactSqlManager.insertContact(contacts, clientUser.getSex());
                                }

                                stopSelf();
                            } catch (InvalidClassException e1) {
                                e1.printStackTrace();
                            }
                            return;
                        }
//                        ToastUtil.showMessage("设置失败,请稍后重试");
                    }
                });
            }

        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
